#include "../../../vcd-common-api/vcd-util.h"
#include "../../../vcd-common-api/vcd-common.h"
#include "../../vcd-message-dialog.h"
#include "../../../vcd-common-api/vcd-conf/vcd-conf.h"
#include "../../vcd-start-window.h"
#include "vcd-login-setting.h"

typedef struct _LoginSetting{
    GtkWidget *modify_passwd_window;
    GtkWidget *passwd_entry_old;
    GtkWidget *passwd_entry_new;
    GtkWidget *passwd_entry_comfirm;
}LoginSetting;

LoginSetting* g_login_setting = NULL;

static void save_sys_passwd(GtkWidget *entry);
static void change_sys_passwd(void);

static void cb_button_grade(GtkToggleButton *button);
static void cb_buttonusrpasswd(GtkToggleButton *button);

static int create_login_setting(void);

static void save_sys_passwd(GtkWidget *entry)
{
    struct vcd_conf* cf = g_vcd_conf;
    LoginSetting* st = g_login_setting;

    const gchar* passwd_old = gtk_entry_get_text((GtkEntry *)entry);
    const gchar* passwd_new = gtk_entry_get_text((GtkEntry *)st->passwd_entry_new);
    const gchar* passwd_confirm = gtk_entry_get_text((GtkEntry *)st->passwd_entry_comfirm);

    if (!strcmp(passwd_old, cf->sys_passwd) == 0) {
        message_box("   旧密码错误！   ");
    } else {
        if (strcmp(passwd_new, passwd_confirm) != 0) {
            message_box("   新密码与确认密码不一致!   ");
            return;
        }

        //save new passwd to conf file
        guint16 l = gtk_entry_get_text_length((GtkEntry*)st->passwd_entry_new);
        if (l > 0) 
            strncpy(cf->sys_passwd, passwd_new, l);
        else 
            memset(cf->sys_passwd, 0, sizeof(cf->sys_passwd));
        update_vcd_conf();
        gtk_widget_destroy(st->modify_passwd_window);
        message_box("   保存成功   "); 
    }
}

static void change_sys_passwd(void)
{
    GtkWidget *fix;

    GtkWidget *label_old;
    GtkWidget *label_new;
    GtkWidget *label_comfirm;

    GtkWidget *passwd_entry_old;
    GtkWidget *passwd_entry_new;
    GtkWidget *passwd_entry_comfirm;

    GtkWidget *btn_yes;
    GtkWidget *btn_no;

    GtkWidget* modify_passwd_window;

    modify_passwd_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_keep_above(GTK_WINDOW(modify_passwd_window), TRUE);
    gtk_window_set_modal((GtkWindow *)modify_passwd_window, TRUE);
    gtk_window_set_position( GTK_WINDOW(modify_passwd_window), GTK_WIN_POS_CENTER_ALWAYS);//居中
    gtk_container_set_border_width(GTK_CONTAINER(modify_passwd_window), 25);
    gtk_window_set_decorated(GTK_WINDOW(modify_passwd_window), FALSE);//无边框

    btn_yes = gtk_button_new_with_label("确定");
    btn_no  = gtk_button_new_with_label("取消");

    label_old     = gtk_label_new("    旧密码:");
    label_new     = gtk_label_new("    新密码:");
    label_comfirm = gtk_label_new("确认新密码:");

    passwd_entry_old     = gtk_entry_new();
    passwd_entry_new     = gtk_entry_new();
    passwd_entry_comfirm = gtk_entry_new();

    gtk_entry_set_visibility((GtkEntry *)passwd_entry_old, FALSE);
    gtk_entry_set_visibility((GtkEntry *)passwd_entry_new, FALSE);
    gtk_entry_set_visibility((GtkEntry *)passwd_entry_comfirm, FALSE);

    gtk_button_set_relief(GTK_BUTTON(btn_no),  GTK_RELIEF_NONE);
    gtk_button_set_relief(GTK_BUTTON(btn_yes), GTK_RELIEF_NONE);

    gtk_widget_set_size_request(btn_yes, 100, -1);
    gtk_widget_set_size_request(btn_no, 100, -1);
    gtk_widget_set_size_request(passwd_entry_old, 210, -1);
    gtk_widget_set_size_request(passwd_entry_new, 210, -1);
    gtk_widget_set_size_request(passwd_entry_comfirm, 210, -1);

    fix    = gtk_fixed_new();
    gtk_fixed_put((GtkFixed *)fix, label_old,  0,   10);
    gtk_fixed_put((GtkFixed *)fix, label_new, 0,   48);
    gtk_fixed_put((GtkFixed *)fix, label_comfirm, 0,   86);
    gtk_fixed_put((GtkFixed *)fix, passwd_entry_old,  75,  0);
    gtk_fixed_put((GtkFixed *)fix, passwd_entry_new, 75,  38);
    gtk_fixed_put((GtkFixed *)fix, passwd_entry_comfirm, 75,  76);
    gtk_fixed_put((GtkFixed *)fix, btn_yes, 75,  115);
    gtk_fixed_put((GtkFixed *)fix, btn_no,  185, 115);

    gtk_container_add(GTK_CONTAINER(modify_passwd_window), fix);

    g_signal_connect_swapped(G_OBJECT(btn_yes), "clicked", G_CALLBACK(save_sys_passwd), passwd_entry_old);
    g_signal_connect_swapped(G_OBJECT(btn_no),  "clicked", G_CALLBACK(gtk_widget_destroy), modify_passwd_window);

    /*            CSS                  */
    set_css(btn_yes, "button{color: white;background: rgb(53, 126, 169);border: 1px solid rgb(40, 94, 142);border-radius: 0px}");
    set_css(btn_yes, "button:hover{border-radius: 0px;background: #3070a9}");
    set_css(btn_no, "button{color: white;background: #5cb85c;border: 1px solid rgb(57, 132, 57);border-radius: 0px}");
    set_css(btn_no, "button:hover{border-radius:0px;background: #449d44;}");
    set_css(modify_passwd_window, "window{border-radius:0px;background-color: rgb(245, 245, 245)}");
    /*            CSS                 */
    gtk_widget_show_all(modify_passwd_window);
    gtk_window_set_position( GTK_WINDOW(modify_passwd_window), GTK_WIN_POS_CENTER_ALWAYS);//居中

    LoginSetting* st = g_login_setting;
    st->modify_passwd_window = modify_passwd_window;
    st->passwd_entry_old     = passwd_entry_old;
    st->passwd_entry_new     = passwd_entry_new;
    st->passwd_entry_comfirm = passwd_entry_comfirm;
}

static void cb_button_grade(GtkToggleButton *button)
{
    g_vcd_conf->enable_grade = (gtk_toggle_button_get_active(button) ? 0 : 1);
    update_vcd_conf();
}

static void cb_buttonusrpasswd(GtkToggleButton *button)
{
    g_vcd_conf->enable_user_passwd = (gtk_toggle_button_get_active(button) ? 0 : 1);
    update_vcd_conf();
    if (g_vcd_conf->enable_user_passwd) {
        gtk_widget_set_sensitive(g_start_window->entry_passwd, TRUE);
        gtk_widget_set_sensitive(g_start_window->remember_passwd, TRUE);
        gtk_widget_set_sensitive(g_start_window->entry_user, TRUE);
        gtk_widget_set_sensitive(g_start_window->auto_login, TRUE);
    } else {
        gtk_widget_set_sensitive(g_start_window->entry_passwd, FALSE);
        gtk_widget_set_sensitive(g_start_window->remember_passwd, FALSE);
        gtk_widget_set_sensitive(g_start_window->entry_user, FALSE);
        gtk_widget_set_sensitive(g_start_window->auto_login, FALSE);
    }
}

static int create_login_setting(void)
{
    //retrun if already create
    if(g_login_setting != NULL){
        return 0;
    }

    g_login_setting = (LoginSetting*)g_malloc(sizeof(LoginSetting));
    if(g_login_setting == NULL){
        return -1;
    }

    return 0;
}

GtkWidget* get_login_setting_fix(void)
{
    GtkWidget *login_set_fix ;
    GtkWidget *forb_usr_passwd_entry;
    GtkWidget *forb_login_grade_button;
    GtkWidget *btn_change_passwd;    
    struct vcd_conf* cf = g_vcd_conf;

    int res = 0;
    res = create_login_setting();
    if(res != 0){
        return NULL;
    }

    login_set_fix = gtk_fixed_new();

    forb_login_grade_button = gtk_check_button_new_with_label("禁用登陆后的高级按钮");
    forb_usr_passwd_entry = gtk_check_button_new_with_label("禁止修改用户名和密码");
    btn_change_passwd = gtk_button_new_with_label("修改密码");

    gtk_button_set_relief(GTK_BUTTON(btn_change_passwd), GTK_RELIEF_NONE);
    gtk_widget_set_tooltip_text(btn_change_passwd, "修改进入系统设置的密码");

    gtk_fixed_put((GtkFixed *)login_set_fix, forb_login_grade_button, 50, 50);
    gtk_fixed_put((GtkFixed *)login_set_fix, forb_usr_passwd_entry, 50, 80);
    gtk_fixed_put((GtkFixed *)login_set_fix, btn_change_passwd, 50, 110);

    gtk_toggle_button_set_active((GtkToggleButton *)forb_usr_passwd_entry, !(gboolean)cf->enable_user_passwd);
    gtk_toggle_button_set_active((GtkToggleButton *)forb_login_grade_button, !(gboolean)cf->enable_grade);

    g_signal_connect(G_OBJECT(btn_change_passwd), "clicked", G_CALLBACK(change_sys_passwd), NULL);
    g_signal_connect(G_OBJECT(forb_usr_passwd_entry), "toggled", G_CALLBACK(cb_buttonusrpasswd), NULL);
    g_signal_connect(G_OBJECT(forb_login_grade_button), "toggled", G_CALLBACK(cb_button_grade), NULL);

    set_css(btn_change_passwd, "button{ color:white;"
            "background:rgb(53,126,169);"
            "border:1px solid rgb(40,94,142);"
            "border-radius: 0px}");

    set_css(btn_change_passwd, "button:hover{border-radius:0px;background:#3070a9}");
    return login_set_fix;
}
